package com.tjtt.tdm.knowledge.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.ParamBean;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.knowledge.bean.KnowFileType;
import com.tjtt.tdm.knowledge.bean.KnowledgeHandOut;
import com.tjtt.tdm.knowledge.bean.KnowledgeManage;
import com.tjtt.tdm.knowledge.bean.KnowledgeType;
import com.tjtt.tdm.knowledge.dao.IKnowFileDao;
import com.tjtt.tdm.knowledge.dao.IKnowledgeDao;
import com.tjtt.tdm.knowledge.dao.IKnowledgeManageDao;
import com.tjtt.tdm.knowledge.service.IKnowledgeService;


/**
 * 
 * @author songxiaoyan
 *
 */
public class KnowledgeServiceImpl implements IKnowledgeService{
	
	private IKnowledgeDao knowledgeDao;
	private IKnowFileDao knowFileDao;
	private IKnowledgeManageDao knowledgeManageDao;

	public List<KnowledgeType> getAllTypes(Map filterMap) throws ServiceException {
		try {
			return knowledgeDao.getAllTypes(filterMap);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}

	public List<KnowledgeType> findByProperty(String KnowledgeTypeName) throws ServiceException {
		try {
			return knowledgeDao.findByProperty("typeName",KnowledgeTypeName);
		} catch (DaoException e) {
//			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	public List<KnowFileType> findByFileName(String KnowFileTypeName) throws ServiceException {
		try {
			return knowFileDao.findByProperty("typeName",KnowFileTypeName);
		} catch (DaoException e) {
//			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	public void saveKnowledgeType(KnowledgeType knowledgeType) throws ServiceException {
		try {
			knowledgeDao.save(knowledgeType);
		} catch (DaoException e) {
//			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}
	public void updateKnowledgeType(KnowledgeType knowledgeType) throws ServiceException {
		try {
			knowledgeDao.update(knowledgeType);
		} catch (DaoException e) {
//			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	public void delKnowledgeType(Long knowledgeTypeId) throws ServiceException {
		try {
			KnowledgeType knowledgeType = knowledgeDao.get(knowledgeTypeId);
			System.out.println(knowledgeType);
			knowledgeDao.remove(knowledgeType);
			
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}

	public Long saveKnowFileType(KnowFileType knowFileType) throws ServiceException {
		return knowledgeDao.savaKnowNew(knowFileType);
	}

	public List<KnowFileType> getKnowFileType(String KnowFileName,Long start,Long limit) throws ServiceException {
		HashMap<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(KnowFileName)) {
			ParamBean p = new ParamBean();
			p.setParam("typeName");
			p.setExpression(" like ");
			p.setValue("%" + KnowFileName + "%");
			params.put("typeName", p);
		}
		try {
			if(start==null)
				return knowFileDao.findByProperties(params);
			else
				return knowFileDao.findByProperties(start, limit, null, null, params);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public List<KnowFileType> getFileTypeName(String fileName, Long id) throws ServiceException {
		try {
			String sql = " select file_documenttype from tdm_file_manage where id="+id+" ";//228,792
			List list = knowFileDao.findBySQL(sql);
			Map map = (Map)list.get(0);
			String ids = (String)map.get("FILE_DOCUMENTTYPE");
			if(ids!= null && ids != ""){
				String [] idss = ids.split(",");
				String sql2 = " select d.id,d.type_name from dict_file_type d where d.id in  ("+ids+") ";
				return knowFileDao.findBySQL(sql2);
			}
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
		return null;
	}
	public PageResult queryKnowledgeManageAfterList(String fileType,Map filterMap) throws ServiceException {
		try {
			return knowledgeManageDao.getAllAfterList(fileType,filterMap);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public PageResult queryKnowledgeManageList(Map filterMap) throws ServiceException {
		try {
			return knowledgeManageDao.getAllList(filterMap);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public PageResult queryKnowledgeManageListByPatentId(String fileType,Map<String, String> filterMap) throws ServiceException {
		try {
			return this.knowledgeManageDao.queryKnowledgeManageListByPatentId(fileType,filterMap);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public List<KnowledgeManage> findByProperty(Map params) throws ServiceException {
		try {
			return knowledgeManageDao.getAllKMList(params);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public void saveKnowledgeManage(KnowledgeManage knowledgemanage) throws ServiceException {
		try {
			knowledgeManageDao.saveOrUpdate(knowledgemanage);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(),e);
		}
	}
	
	public KnowFileType findKnowledgeByName(String typeName) throws ServiceException {
		return this.knowledgeManageDao.getKnowledgeByName(typeName);
	}
	public IKnowledgeDao getKnowledgeDao() {
		return knowledgeDao;
	}
	
	public void setKnowledgeDao(IKnowledgeDao knowledgeDao) {
		this.knowledgeDao = knowledgeDao;
	}
	
	public IKnowFileDao getKnowFileDao() {
		return knowFileDao;
	}
	
	public void setKnowFileDao(IKnowFileDao knowFileDao) {
		this.knowFileDao = knowFileDao;
	}

	public IKnowledgeManageDao getKnowledgeManageDao() {
		return knowledgeManageDao;
	}

	public void setKnowledgeManageDao(IKnowledgeManageDao knowledgeManageDao) {
		this.knowledgeManageDao = knowledgeManageDao;
	}

	public PageResult queryUsersByRoleId(Map filterMap) throws ServiceException {
		return knowledgeManageDao.queryUsersByRoleId(filterMap);
	}

	public void delKnowledgeManage(Long knowledgeTypeId) throws ServiceException {
		knowledgeManageDao.delKnowledgeManage(knowledgeTypeId);
	}
	/**
	 * 判断文件类别下是否有文件
	 * @throws IOException
	 * @throws ServiceException void
	 * @author 张海娜
	 * @date 2012-10-19 下午06:30:38
	 */
	public PageResult getCountKnowledgeTypeByFileType(Long id,Map<String, String> filterMap) throws ServiceException  {
		return this.knowledgeManageDao.getCountKnowledgeTypeByFileType(id,filterMap);
	}
	/**
	 * 查询所有的标签
	 * @param filterMap
	 * @return PageResult
	 * @author 张海娜
	 * @date 2012-10-26 下午02:31:39
	 */
	public PageResult getAllKnowFileType(Map<String, String> filterMap) {
		return this.knowledgeManageDao.getAllKnowFileType(filterMap);
	}

    public Long savaKnowNew(KnowFileType knowFileType)
    		throws ServiceException {
    	return this.knowledgeDao.savaKnowNew(knowFileType);
    }

	
	public KnowFileType findknowFileType(Long biaoqianid) {
		return this.knowledgeDao.findknowFileType(biaoqianid);
	}

	
	public void saveKnowledgeHandOut(KnowledgeHandOut knowledgeHandOut)throws ServiceException {
		try {
			knowledgeDao.save(knowledgeHandOut);
		} catch (DaoException e) {
//			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
	}

	
	public PageResult getAllKnowledgeManageShouCangExec(
			Map<String, String> filterMap) {
		return knowledgeManageDao.getAllKnowledgeManageShouCangExec(filterMap);
	}

	
	public KnowledgeManage findFileAddressById(long id) {
		return knowledgeManageDao.findFileAddressById(id);
	}

	
	public PageResult getAllKnowledgeManageAfterByUserIdExec(
			Map<String, String> filterMap) {
		return knowledgeManageDao.getAllKnowledgeManageAfterByUserIdExec(filterMap);
	}

	
	public PageResult getAllKnowledgeManageByPatentIdByUserIdExec(
			String fileType, Map<String, String> filterMap) {
		return knowledgeManageDao.getAllKnowledgeManageByPatentIdByUserIdExec(fileType,filterMap);
	}

	
	public String findFenFa(String uid, String id) {
		return knowledgeManageDao.findFenFa(uid,id);
	}

	
	public PageResult searchFenFaUserExec(Map<String, String> filterMap) {
		return knowledgeManageDao.searchFenFaUserExec(filterMap);
	}

	
	public List<KnowledgeHandOut> findUserids(String knowledgeid) {
		return knowledgeManageDao.findUserids(knowledgeid);
	}

	
	public List<KnowledgeHandOut> findKnowledgeHanderOut(long id) {
		return knowledgeManageDao.findKnowledgeHanderOut(id);
	}

	
	public void delknowledgeHandOut(long id) {
		knowledgeManageDao.delknowledgeHandOut(id);
	}


	

	

	

	

	

}
